#!/usr/bin/env python
# coding:utf-8
import pymysql, time
import random

date = time.strftime('%Y-%m-%d %H:%M')
def select_mysql(sql):
    conn = pymysql.connect(host='127.0.0.1', user='root', passwd='UYCC3JpsVxcHElmTaiyR', db='zabbix',
                           charset='UTF8')

    cur = conn.cursor()
    reCount = cur.execute(sql)

    nRet = cur.fetchall()
    cur.close()

    conn.close()
    return nRet

def insert_mysql(sql):
    conn = pymysql.connect(host='121.201.68.21', port=3307, user='jiang', passwd='jiangwenhui', db='daxiangzhanshi',

                           charset='UTF8')

    cur = conn.cursor()

    cur.execute(sql)
    nRet = cur.fetchall()
    conn.commit()

    cur.close()

    conn.close()
    return nRet


if __name__ == "__main__":
    select = "SELECT SUM(value),itemid FROM zabbix.history_uint WHERE   clock>=%s and itemid in (32795) group by itemid" % (
    time.time() - 300)
    data = select_mysql(select)[0][0]

    sj = date[11:]
    sj = int(sj[:-3])
    # print(sj)


    ds = data*21
    print(ds)
    # 登陆 = random.randint((7000 * 300 * 40 / 100 - (7000 * 300 * 40 / 100) * 5 / 100) * 40 / 100,
    #                     (7000 * 300 * 40 / 100 + (7000 * 300 * 40 / 100) * 5 / 100) * 40 / 100)
    # 后台 = random.randint((7000 * 300 * 20 / 100 - (7000 * 300 * 20 / 100) * 5 / 100) * 40 / 100,
    #                 (7000 * 300 * 20 / 100 + (7000 * 300 * 20 / 100) * 5 / 100) * 40 / 100)


    sql = "insert into app01_jiaywpv values (null,'%s','%s','%s','%s')" % ('电商', ds, date[-6:], date)
    insert_mysql(sql)
    # sql = "insert into app01_jiaywpv values (null,'%s','%s','%s','%s')" % ('登陆', 登陆, date[-6:], date)
    # insert_mysql(sql)
    # sql = "insert into app01_jiaywpv values (null,'%s','%s','%s','%s')" % ('后台', 后台, date[-6:], date)
    # insert_mysql(sql)
    print('ok')
